﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MiruPublic;

namespace MangaKurosagi
{
    public class LeadChap:Website
    {
        private List<ItemChap> _ListChap;
        private string _content;
        public List<ItemChap> ListStories
        {
            get { return _ListChap; }
            set
            {
                _ListChap = value;
                LeadChapCompletedEventArgs args = new LeadChapCompletedEventArgs();
                args.listResult = _ListChap;
                args.content = _content;
                OnListUpdateSucess(args);
            }
        }

        public event EventHandler<LeadChapCompletedEventArgs> ListUpdateSucess;
        protected virtual void OnListUpdateSucess(LeadChapCompletedEventArgs e)
        {
            EventHandler<LeadChapCompletedEventArgs> handler = ListUpdateSucess;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected override void OnGetContentSucess(DownloadStringCompletedEventArgs e)
        {
            base.OnGetContentSucess(e);
            _content = e.Result;
            ListStories = GetListChap(Utils.GetStringBetweenText(e.Result, "<div class=\"danhsach\">", e.Result.IndexOf("<a name=\"formcomment\">") > 0 ? "<a name=\"formcomment\">" : "<div class=\"paging-div\""));
        }

        private List<ItemChap> GetListChap(string content)
        {
            List<ItemChap> Result = new List<ItemChap>();
            string CheckContent = content;
            string line = "";
            int index = 0;
            line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            while (line != "")
            {
                ItemChap item = new ItemChap();
                item.Url = "http://m.blogtruyen.com" + Utils.GetStringBetweenText(line, "\"", "\"");
                item.Title = HttpUtility.HtmlDecode(Utils.GetStringBetweenText(line, "title=\"", "\""));
                if (item.Title == "") return Result;
                item.index = index;
                Result.Add(item);
                index++;
                CheckContent = Utils.GetStringBetweenText(CheckContent, line, "");

                line = Utils.GetStringBetweenText(CheckContent, "<a href=", "/a>");
            }
            return Result;
        }
    }
    public class LeadChapCompletedEventArgs : EventArgs
    {
        public List<ItemChap> listResult;
        public string content;
    }
}
